class Admin::OrdersController < Admin::ApplicationController
  load_resource
  authorize_resource
  before_filter :set_order, only: [:show, :edit, :update, :destroy]
  before_filter :base_breadcrumb
  before_filter :name_breadcrumb, only: [:show, :edit]

  # GET /admin/orders
  def index
    add_breadcrumb t('common.list'), request.path
    @orders = initialize_grid(Order.where(user_id:current_user.id), page: params[:page], order: :created_at, order_direction: :desc)
  end

  # GET /admin/orders/1
  def show
  end

  # GET /admin/orders/new
  def new
    add_breadcrumb t('common.create'), request.path
    @order = Order.new
  end

  # POST /admin/orders
  def create
    @order = Order.new(order_params)

    respond_to do |format|
      if @order.save
        @order.update_attributes(user_id:current_user.id,order_date:Time.now.strftime('%Y-%m-%d').to_s)
        format.html { redirect_to admin_order_path(@order.id), notice: create_success_notice(:order) }
        format.json { render :show, status: :created, location: @order }
      else
        format.html { render :new }
        format.json { render json: @order.errors, status: :unprocessable_entity }
      end
    end
  end

  private
  # Use callbacks to share common setup or constraints between actions.
  def set_order
    if params[:action] == 'show'
      @order = Order.find(params[:id])
    else
      @order = Order.find(params[:id])
    end
  end
  def base_breadcrumb
    add_breadcrumb (t 'activerecord.attributes.order.admin_title'), admin_orders_path
  end

  def name_breadcrumb
    add_breadcrumb @order.name, admin_order_path(@order.id)
  end

  def order_params
    params.require(:order).permit(:sr,:name,:phone)
  end


end
